# Get all the variables from ANT
name = '${ANT_SERVER_NAME}'
cp = '${ANT_MS_CP}'
arg = '${ANT_MS_ARG}'
vendor = '${ANT_JVM_VENDOR}'
java_home = '${ANT_JAVA_HOME}'
as_ip = '${ANT_AS_IP}'
as_port = ${ANT_AS_PORT}
as_pass = '${ANT_AS_PASS}'
domain_path = '${ANT_DOMAIN_HOME}/${ANT_DOMAIN_NAME}' 

#Server
server_list = ${ANT_SERVER_NAMES}
server_ip_list = ${ANT_MS_IPS}
server_port_list = ${ANT_MS_PORTS}

# Machine
machine_host_name = '${ANT_MACHINE_HOSTNAME}'

# Node Manager
nm_home = '${ANT_NM_HOME}'
nm_type = '${ANT_NM_TYPE}'
nm_port = ${ANT_NM_PORT}
nm_ip = '${ANT_NM_IP}'

username = '${ANT_AS_USER}'
password = '${ANT_AS_PASS}'
url = '${ANT_AS_URL}'

# clusters
resCluster = '${ANT_RES_CLUSTER_NAME}'
bkgCluster = '${ANT_BKG_CLUSTER_NAME}'
clusterList= bkgCluster,resCluster

def enroll():
    print 'Enrolling the machine...'
    connect(username, password, url)
    nmEnroll(domain_path, nm_home)
    
def addMachine():
    print 'Adding the machine...'
    edit()
    startEdit()
    cd('/')
    
    cmo.createUnixMachine(machine_host_name)

    cd('/Machines/' + machine_host_name + '/NodeManager/' + machine_host_name)
    cmo.setNMType(nm_type)
    cmo.setListenPort(nm_port)
    cmo.setListenAddress(nm_ip)
    cmo.setDebugEnabled(false)
    activate()
    
def addServer(server, server_ip, server_port):   
    print 'Adding Server...'
    
    try:
        edit()
        startEdit()
        cd('/')
        cmo.createServer(server)
    
        # Set the server settings
        cd('/Servers/' + server)
        print 'Setting listen address to ' + server_ip
        cmo.setListenAddress(server_ip)
        
        print 'Setting port to ' + server_port
        cmo.setListenPort(int(server_port))
    
        activate()
        print 'Created server: ' + server
    except:
        print 'Error Creating server' + server
        
def setServerStart(server, server_ip, server_port):
    try:    
        startEdit()
        # cd('/Servers/'+ server + '/ServerStart/' + server)    
        
        print 'Creating server start'    
        # cd('/Servers/' + server)
        # create(server, 'ServerStart')
        cd('/Servers/' + server + '/ServerStart/' + server)    
    
        print 'Setting Java Vendor'
        cmo.setJavaVendor(vendor)
        
        print 'Setting Java Home'
        cmo.setJavaHome(java_home)
        
        print 'Setting Arguments'
        cmo.setArguments(arg)
        
        print 'Setting Classpath'
        cmo.setClassPath(cp)
        
        print 'Activating'
        activate()
        
        print 'Done Adding Server'
    except:
        print 'Error setting server start settings'

def setServerMachine(server, machine):
    try:
        print 'Setting machine to ' + machine
        edit()
        startEdit()
        cd('/Servers/' + server)
        cmo.setMachine(getMBean('/Machines/' + machine))    
        activate()
        print 'Successfully set machine'
    except:
        print 'Failed to set machine'
        
def setServerCluster(server, cluster):
    edit()
    startEdit()    
    cd('/Servers/' + server)
    cmo.setCluster(getMBean('/Clusters/' + cluster))
    activate()

def createCluster(cluster): 
    try:
        edit()
        startEdit()
        cd('/')
        cmo.createCluster(cluster)
        activate()
    except:
        print 'Failed to create cluster ' + cluster

def setupOverload(server):
    try:
        cd('/Servers/' + server '/OverloadProtection/' + server)
        cmo.setPanicAction('system-exit')
        cmo.setFreeMemoryPercentHighThreshold(30)
        cmo.setFailureAction('force-shutdown')
        cmo.setFreeMemoryPercentLowThreshold(10)
        cmo.createServerFailureTrigger()
        
        cd('/Servers/' + server + '/OverloadProtection/' + server + '/ServerFailureTrigger/' + server)
        cmo.setMaxStuckThreadTime(120)
        cmo.setStuckThreadCount(4)
    except:
        print 'Failed to set Overlaod protection for ' + server        
#################################################
# Main
#################################################            
connect(username, password, url)    
try:
    addMachine()
except:
    print "Failed to add machine"


createCluster(resCluster)
createCluster(bkgCluster)

        
try:
    enroll()
except:
    print "Failed to enroll this machine"
    
server_index = 0;    
for server in server_list:    
    addServer(server, server_ip_list[server_index], server_port_list[server_index])
    setServerStart(server, server_ip_list[server_index], server_port_list[server_index])
    setServerMachine(server, machine_host_name)
    setServerCluster(server,clusterList[server_index])
    
    server_index = server_index + 1;

